package sap.gui;

import data.*;
import exceptions.*;
import sap.provider.*;

import javax.swing.*;
/*
 * PSPManager.java
 *
 * Created on 10 de noviembre de 2007, 12:03 PM
 */

/**
 *
 * @author  Enrique Garcia
 */
public class PSPManager extends javax.swing.JPanel{
    
    /** Creates new form PSPManager */
    public PSPManager(CManager cm) {
        initComponents();
        init();
        this.cm=cm;
    }
    
public void init()
    {
    jPanel1.setVisible(false);
    run.setVisible(false);
    jTextField1.setEditable(false);
    jTextField2.setEditable(false);
    }


public void init(String psp)
    {
    this.psp = psp;
    jTextField1.setText(cm.project);
    jPanel1.setVisible(false);
    run.setVisible(false);
    jTextField1.setEditable(false);
    jTextField2.setEditable(false);
    jTextField2.setText(this.psp);
    }


    public void clear()
    {
        jTextField3.setText("");
		jTextField4.setText("");
        jTextField5.setText("");
        jTextField6.setText("");
		jTextField7.setText("");
        jTextField8.setText("");
        jTextField9.setText("");
		jTextField10.setText("");
        jTextField11.setText("");
        jTextField12.setText("");
        jTextField13.setText("");
        jTextField14.setText("");
        jTextField15.setText("");
		jTextField16.setText("");
        jTextField17.setText("");
        jTextField18.setText("");
		jTextField19.setText("");
        jTextField20.setText("");
        jTextField21.setText("");
    }

    private void setDialogFromStructure(PSPElement element) {
        jTextField1.setText(element.project_definition);
        jTextField2.setText(element.wbs_element);
        jTextField3.setText(element.description);
		jTextField4.setText(element.short_id);
        jTextField5.setText(element.responsible_no);
        jTextField6.setText(element.applicant_no);
		jTextField7.setText(element.comp_code);
        jTextField8.setText(element.bus_area);
        jTextField9.setText(element.co_area);
		jTextField10.setText(element.profit_ctr);
        jTextField11.setText(element.proj_type);
        jTextField12.setText(element.network_assignment);
        jTextField13.setText(element.costing_sheet);
        jTextField14.setText(element.overhead_key);
        jTextField15.setText(element.calendar);
		jTextField16.setText(element.priority);
        jTextField17.setText(element.equipment);
        jTextField18.setText(element.functional_location);
		jTextField19.setText(element.currency);
        jTextField20.setText(element.currency_iso);
        jTextField21.setText(element.plant);

	}


    private PSPElement getListFromDialog() {

        PSPElement element = new PSPElement();
        element.project_definition = jTextField1.getText();
        element.wbs_element = jTextField2.getText();
		element.description = jTextField3.getText();
        element.short_id = jTextField4.getText();
        element.responsible_no = jTextField5.getText();
        element.applicant_no = jTextField6.getText();
        element.comp_code = jTextField7.getText();
        element.bus_area = jTextField8.getText();
        element.co_area = jTextField9.getText();
        element.profit_ctr = jTextField10.getText();
        element.proj_type = jTextField11.getText();
        element.network_assignment = jTextField12.getText();
        element.costing_sheet = jTextField3.getText();
        element.overhead_key = jTextField4.getText();
        element.calendar = jTextField15.getText();
        element.priority = jTextField16.getText();
        element.equipment = jTextField17.getText();
        element.functional_location = jTextField18.getText();
        element.currency = jTextField19.getText();
        element.currency_iso= jTextField20.getText();
        element.plant = jTextField21.getText();



                return element;

    }


    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        Tipos = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        run = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jTextField4 = new javax.swing.JTextField();
        jTextField5 = new javax.swing.JTextField();
        jTextField6 = new javax.swing.JTextField();
        jTextField7 = new javax.swing.JTextField();
        jTextField8 = new javax.swing.JTextField();
        jTextField9 = new javax.swing.JTextField();
        jTextField10 = new javax.swing.JTextField();
        jTextField11 = new javax.swing.JTextField();
        jTextField12 = new javax.swing.JTextField();
        jTextField13 = new javax.swing.JTextField();
        jTextField14 = new javax.swing.JTextField();
        jLabel17 = new javax.swing.JLabel();
        jLabel18 = new javax.swing.JLabel();
        jLabel19 = new javax.swing.JLabel();
        jLabel20 = new javax.swing.JLabel();
        jLabel21 = new javax.swing.JLabel();
        jLabel22 = new javax.swing.JLabel();
        jLabel23 = new javax.swing.JLabel();
        jTextField15 = new javax.swing.JTextField();
        jTextField16 = new javax.swing.JTextField();
        jTextField17 = new javax.swing.JTextField();
        jTextField18 = new javax.swing.JTextField();
        jTextField19 = new javax.swing.JTextField();
        jTextField20 = new javax.swing.JTextField();
        jTextField21 = new javax.swing.JTextField();
        ok = new javax.swing.JButton();
        cancel = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();

        setBackground(new java.awt.Color(0, 0, 0));
        setLayout(null);

        jLabel2.setForeground(new java.awt.Color(255, 255, 255));
        jLabel2.setText("Choose one function");
        add(jLabel2);
        jLabel2.setBounds(10, 10, 100, 20);

        Tipos.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Choose one function", "Create PSP (WBS) Element", "Edit PSP Element", "Delete PSP Element" }));
        Tipos.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                TiposItemStateChanged(evt);
            }
        });
        add(Tipos);
        Tipos.setBounds(10, 30, 180, 20);

        jLabel3.setForeground(new java.awt.Color(255, 255, 255));
        jLabel3.setText("PROJECT_DEFINITION");
        add(jLabel3);
        jLabel3.setBounds(230, 10, 140, 14);
        add(jTextField1);
        jTextField1.setBounds(230, 30, 120, 20);

        run.setBackground(new java.awt.Color(0, 0, 0));
        run.setText("Run");
        run.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                runActionPerformed(evt);
            }
        });
        add(run);
        run.setBounds(620, 20, 130, 30);

        jPanel1.setBackground(new java.awt.Color(0, 0, 0));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "PSP Details", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(255, 255, 255))); // NOI18N
        jPanel1.setForeground(new java.awt.Color(255, 255, 255));
        jPanel1.setLayout(null);

        jLabel5.setForeground(new java.awt.Color(255, 255, 255));
        jLabel5.setText("DESCRIPTION");
        jPanel1.add(jLabel5);
        jLabel5.setBounds(70, 40, 140, 14);

        jLabel6.setForeground(new java.awt.Color(255, 255, 255));
        jLabel6.setText("SHORT_ID");
        jPanel1.add(jLabel6);
        jLabel6.setBounds(70, 70, 140, 14);

        jLabel7.setForeground(new java.awt.Color(255, 255, 255));
        jLabel7.setText("RESPONSIBLE_NO");
        jPanel1.add(jLabel7);
        jLabel7.setBounds(70, 100, 140, 14);

        jLabel8.setForeground(new java.awt.Color(255, 255, 255));
        jLabel8.setText("APPLICANT_NO");
        jPanel1.add(jLabel8);
        jLabel8.setBounds(70, 130, 140, 14);

        jLabel9.setForeground(new java.awt.Color(255, 255, 255));
        jLabel9.setText("COMP_CODE");
        jPanel1.add(jLabel9);
        jLabel9.setBounds(70, 160, 140, 14);

        jLabel10.setForeground(new java.awt.Color(255, 255, 255));
        jLabel10.setText("BUS_AREA");
        jPanel1.add(jLabel10);
        jLabel10.setBounds(70, 190, 140, 14);

        jLabel11.setForeground(new java.awt.Color(255, 255, 255));
        jLabel11.setText("CO_AREA");
        jPanel1.add(jLabel11);
        jLabel11.setBounds(70, 220, 140, 14);

        jLabel12.setForeground(new java.awt.Color(255, 255, 255));
        jLabel12.setText("PROFIT_CTR");
        jPanel1.add(jLabel12);
        jLabel12.setBounds(70, 250, 140, 14);

        jLabel13.setForeground(new java.awt.Color(255, 255, 255));
        jLabel13.setText("PROJ_TYPE");
        jPanel1.add(jLabel13);
        jLabel13.setBounds(70, 280, 140, 14);

        jLabel14.setForeground(new java.awt.Color(255, 255, 255));
        jLabel14.setText("NETWORK_ASSIGNMENT");
        jPanel1.add(jLabel14);
        jLabel14.setBounds(70, 310, 140, 14);

        jLabel15.setForeground(new java.awt.Color(255, 255, 255));
        jLabel15.setText("COSTING_SHEET");
        jPanel1.add(jLabel15);
        jLabel15.setBounds(70, 340, 140, 14);

        jLabel16.setForeground(new java.awt.Color(255, 255, 255));
        jLabel16.setText("OVERHEAD_KEY");
        jPanel1.add(jLabel16);
        jLabel16.setBounds(70, 370, 140, 14);
        jPanel1.add(jTextField3);
        jTextField3.setBounds(200, 40, 170, 20);
        jPanel1.add(jTextField4);
        jTextField4.setBounds(200, 70, 170, 20);
        jPanel1.add(jTextField5);
        jTextField5.setBounds(200, 100, 170, 20);
        jPanel1.add(jTextField6);
        jTextField6.setBounds(200, 130, 170, 20);
        jPanel1.add(jTextField7);
        jTextField7.setBounds(200, 160, 170, 20);
        jPanel1.add(jTextField8);
        jTextField8.setBounds(200, 190, 170, 20);
        jPanel1.add(jTextField9);
        jTextField9.setBounds(200, 220, 170, 20);
        jPanel1.add(jTextField10);
        jTextField10.setBounds(200, 250, 170, 20);
        jPanel1.add(jTextField11);
        jTextField11.setBounds(200, 280, 170, 20);
        jPanel1.add(jTextField12);
        jTextField12.setBounds(200, 310, 170, 20);
        jPanel1.add(jTextField13);
        jTextField13.setBounds(200, 340, 170, 20);
        jPanel1.add(jTextField14);
        jTextField14.setBounds(200, 370, 170, 20);

        jLabel17.setForeground(new java.awt.Color(255, 255, 255));
        jLabel17.setText("CALENDAR");
        jPanel1.add(jLabel17);
        jLabel17.setBounds(460, 40, 100, 14);

        jLabel18.setForeground(new java.awt.Color(255, 255, 255));
        jLabel18.setText("PRIORITY");
        jPanel1.add(jLabel18);
        jLabel18.setBounds(460, 70, 100, 14);

        jLabel19.setForeground(new java.awt.Color(255, 255, 255));
        jLabel19.setText("EQUIPMENT");
        jPanel1.add(jLabel19);
        jLabel19.setBounds(460, 100, 100, 14);

        jLabel20.setForeground(new java.awt.Color(255, 255, 255));
        jLabel20.setText("FUNCTIONAL_LOCATION");
        jPanel1.add(jLabel20);
        jLabel20.setBounds(460, 130, 100, 14);

        jLabel21.setForeground(new java.awt.Color(255, 255, 255));
        jLabel21.setText("CURRENCY");
        jPanel1.add(jLabel21);
        jLabel21.setBounds(460, 160, 100, 14);

        jLabel22.setForeground(new java.awt.Color(255, 255, 255));
        jLabel22.setText("CURRENCY_ISO");
        jPanel1.add(jLabel22);
        jLabel22.setBounds(460, 190, 100, 14);

        jLabel23.setForeground(new java.awt.Color(255, 255, 255));
        jLabel23.setText("PLANT");
        jPanel1.add(jLabel23);
        jLabel23.setBounds(460, 220, 100, 14);
        jPanel1.add(jTextField15);
        jTextField15.setBounds(570, 40, 160, 20);
        jPanel1.add(jTextField16);
        jTextField16.setBounds(570, 70, 160, 20);
        jPanel1.add(jTextField17);
        jTextField17.setBounds(570, 100, 160, 20);
        jPanel1.add(jTextField18);
        jTextField18.setBounds(570, 130, 160, 20);
        jPanel1.add(jTextField19);
        jTextField19.setBounds(570, 160, 160, 20);
        jPanel1.add(jTextField20);
        jTextField20.setBounds(570, 190, 160, 20);
        jPanel1.add(jTextField21);
        jTextField21.setBounds(570, 220, 160, 20);

        ok.setText("Ok");
        ok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okActionPerformed(evt);
            }
        });
        jPanel1.add(ok);
        ok.setBounds(260, 440, 100, 23);

        cancel.setText("Cancel");
        cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelActionPerformed(evt);
            }
        });
        jPanel1.add(cancel);
        cancel.setBounds(410, 440, 100, 23);

        add(jPanel1);
        jPanel1.setBounds(10, 60, 810, 490);

        jLabel4.setForeground(new java.awt.Color(255, 255, 255));
        jLabel4.setText("PSP_ELEMENT");
        add(jLabel4);
        jLabel4.setBounds(430, 10, 140, 14);
        add(jTextField2);
        jTextField2.setBounds(430, 30, 100, 20);
    }// </editor-fold>//GEN-END:initComponents

    private void TiposItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_TiposItemStateChanged

        JComboBox cb = (JComboBox)evt.getSource();

        src = cb.getSelectedIndex();

        switch(src) {
            case 0:
                break;
            case 1:
                this.clear();
                this.init();
                jPanel1.setVisible(true);
                break;
            case 2:
                this.clear();
                this.init();
                run.setVisible(true);
                break;
            case 3:
                this.clear();
                this.init();
                run.setVisible(true);
                break;
                

        }

    }//GEN-LAST:event_TiposItemStateChanged

    private void okActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okActionPerformed

        if(src==1) {
            try {
                PSP.create(getListFromDialog());
            } catch (ReturnException ex) {
                JOptionPane.showMessageDialog(cm, ex.getMessage(), "Exception", JOptionPane.ERROR_MESSAGE);
                return;
            }
            this.clear();
            this.init();
        } else
            if(src==2) {
                try {
                    PSP.maintain(getListFromDialog());
                } catch (ReturnException ex) {
                    JOptionPane.showMessageDialog(cm, ex.getMessage(), "Exception", JOptionPane.ERROR_MESSAGE);
                }
                this.clear();
                this.init();
            }

        Tipos.setSelectedIndex(0);

    }//GEN-LAST:event_okActionPerformed

    private void cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelActionPerformed

        this.clear();
        this.init();
        Tipos.setSelectedIndex(0);
}//GEN-LAST:event_cancelActionPerformed

    private void runActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_runActionPerformed

            

        if(src==0) {
            JOptionPane.showMessageDialog(cm, "You need to select one " +
                    "option", "Error", JOptionPane.ERROR_MESSAGE);
            this.clear();
            this.init();

        } else
            if(src==2) {

                try {
                        PSPElement element = PSP.get(jTextField1.getText(), jTextField2.getText());
                        this.setDialogFromStructure(element);
                        jPanel1.setVisible(true);
                        run.setVisible(false);
                    
                } catch (ReturnException ex) {
                    JOptionPane.showMessageDialog(cm, ex.getMessage(), "Exception", JOptionPane.ERROR_MESSAGE);
                }

            } else
                
            if(src==3) {
                try {
                        PSP.delete(jTextField1.getText(), jTextField2.getText());
                        JOptionPane.showMessageDialog(cm, "PSP Removed", "Info", JOptionPane.INFORMATION_MESSAGE);
                } catch (ReturnException ex) {
                    JOptionPane.showMessageDialog(cm, ex.getMessage(), "Exception", JOptionPane.ERROR_MESSAGE);
                }

            }
}//GEN-LAST:event_runActionPerformed

    
    

    
    private CManager cm;
    private String ID_Salon;
    private String NumEmp;
    private String date;
    private String duration;
    private String time;
    private String timei;
    private String timef;
    private String ID_Res;
    private String ID_Secc;
    private String res;
    private String clave;
    protected static final String CROWNER="CROWNER";
    protected static final String RESERVE="RESERVE";
    protected static final String FREE="FREE";
    private java.util.StringTokenizer tokenizer;
    private boolean canReserve=false;
    private int src = -1;
    protected String psp;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox Tipos;
    private javax.swing.JButton cancel;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JLabel jLabel22;
    private javax.swing.JLabel jLabel23;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField10;
    private javax.swing.JTextField jTextField11;
    private javax.swing.JTextField jTextField12;
    private javax.swing.JTextField jTextField13;
    private javax.swing.JTextField jTextField14;
    private javax.swing.JTextField jTextField15;
    private javax.swing.JTextField jTextField16;
    private javax.swing.JTextField jTextField17;
    private javax.swing.JTextField jTextField18;
    private javax.swing.JTextField jTextField19;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField20;
    private javax.swing.JTextField jTextField21;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    private javax.swing.JTextField jTextField6;
    private javax.swing.JTextField jTextField7;
    private javax.swing.JTextField jTextField8;
    private javax.swing.JTextField jTextField9;
    private javax.swing.JButton ok;
    private javax.swing.JButton run;
    // End of variables declaration//GEN-END:variables
    
}
